package ui;

import java.awt.Window;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.util.ArrayList;
import java.util.Scanner;

public class backButton
	{
		
		private Scanner reader;
		
		private ArrayList<String> lines;
		private MenuUI backMenu;
		private CityUI backCity;
		private CountryUI backCountry;
		private AboutUI backAbout;
		private CustomerUI backCustomer;
		private EmployeeUI backEmployee;
		private SupplierUI backSupplier;
		private SubContractorUI backSubcontractor;
		private ItemUI backItem;
		private OfferUI backOffer;
		private ProjectUI backProject;
		private RequestsUI backRequest;
		private StatisticsUI backStatistic;
		private HelpUI backHelp;
		
		private FileWriter fWriter = null;
		private BufferedWriter writer = null; 
		
		
		public backButton()
			{
				reader = null;
								
				try
					{
						reader = new Scanner(new File("config/lastWindow.txt"));
					}
					catch (FileNotFoundException e)
					{
						e.printStackTrace();
					}
				
				lines = new ArrayList<String>();
				
			}
		
		public int getNumber()
			{
				while (reader.hasNextLine())
					{
					lines.add(reader.nextLine());
					}

				reader.close();
				System.out.println(lines.get(lines.size()-2));
				return Integer.valueOf( lines.get(lines.size()-2));
			}
		public void eraseLast()
			{
				try
					{
				byte b;
				RandomAccessFile f = new RandomAccessFile(new File("config/lastWindow.txt"), "rw");
				long length = f.length() - 1;
				do {                     
				  length -= 2;
				  f.seek(length);
				  b = f.readByte();
				} while(b != 10);
				f.setLength(length+1);
				f.close();
					}
				catch(Exception e)
					{
						
					}
			}
		public void setNumber(String number)
			{
				try {
				  fWriter = new FileWriter("config/lastWindow.txt", true);
				  writer = new BufferedWriter(fWriter);
				  
						  writer.write(number);
						  writer.newLine();
				  
				   writer.close();
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		public void clearFile()
			{
				try
					{
						fWriter = new FileWriter("config/lastWindow.txt");
						writer = new BufferedWriter(fWriter);
						  
						writer.write("");
						
				writer.close();
					} catch (IOException e)
						{
							// TODO Auto-generated catch block
							e.printStackTrace();
						}
			}
		
		public void goBack(int windowNumber)
			{
				eraseLast();
				for (Window frame : Window.getWindows())
					{
						if (frame.isActive())
						{
							frame.setVisible(false);
						}
					}
				switch(windowNumber)
				{
					
					case 1:
					if(backMenu == null)
						backMenu = new MenuUI();
						backMenu.display();
					break;
					
					case 2:
						if(backCity == null)
							backCity = new CityUI();
							backCity.display();
						break;
						
					case 3:
						if(backCountry == null)
							backCountry = new CountryUI();
						backCountry.display();
						break;
					case 4:
						if(backCustomer == null)
							backCustomer = new CustomerUI();
						backCustomer.display();
						break;
					case 5:
						if(backSupplier == null)
							backSupplier = new SupplierUI();
						backSupplier.display();
						break;
					case 6:
						if(backAbout == null)
							backAbout = new AboutUI();
						backAbout.display();
						break;
					case 7:
						if(backEmployee == null)
							backEmployee = new EmployeeUI();
						backEmployee.display();
						break;
					case 8:
						if(backSubcontractor == null)
							backSubcontractor = new SubContractorUI();
						backSubcontractor.display();
						break;
					case 9:
						if(backItem == null)
							backItem = new ItemUI();
						backItem.display();
						break;
					case 10:
						if(backOffer == null)
							backOffer = new OfferUI();
						backOffer.display();
						break;
					case 11:
						if(backProject == null)
							backProject = new ProjectUI();
						backProject.display();
						break;
					case 12:
						if(backRequest == null)
							backRequest = new RequestsUI();
						backRequest.display();
						break;
					case 13:
						if(backStatistic == null)
							backStatistic = new StatisticsUI();
						backStatistic.display();
						break;
					case 14:
						if(backHelp == null)
							backHelp = new HelpUI();
						backHelp.display();
				}
			}
		
		
		
	}
